﻿package com.taihua_tech.basic.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taihua_tech.basic.dao.NoteDao;
import com.taihua_tech.basic.entity.Note;
import com.taihua_tech.basic.util.NoteResult;
import com.taihua_tech.basic.util.NoteUtil;
 

@Service("noteService")
@Transactional
public class NoteServiceImpl implements NoteService{
	@Resource
	private NoteDao noteDao;
	
	public NoteResult loadBookNotes(String bookId) {
		List<Note> list = 
			noteDao.findByBookId(bookId);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("查询笔记列表成功");
		result.setData(list);
		return result;
	}

	public NoteResult loadNote(String noteId) {
		Note note = noteDao.findById(noteId);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("查询笔记信息成功");
		result.setData(note);
		return result;
	}

	public NoteResult updateNote(
		String noteId, String noteTitle,
		String noteBody) {
		//重名检测
		//保存笔记操作
		Note note = new Note();
		note.setCn_note_id(noteId);//笔记ID
		note.setCn_note_title(noteTitle);//笔记标题
		note.setCn_note_body(noteBody);//笔记内容
		long time = System.currentTimeMillis();
		note.setCn_note_last_modify_time(time);//最后修改时间
		noteDao.dynamicUpdate(note);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("保存笔记成功");
		return result;
	}

	public NoteResult addNote(
		String userId, String bookId, String noteTitle) {
		Note note = new Note();
		note.setCn_user_id(userId);
		note.setCn_notebook_id(bookId);
		note.setCn_note_title(noteTitle);
		String noteId = NoteUtil.createId();
		note.setCn_note_id(noteId);
		Long time = System.currentTimeMillis();
		note.setCn_note_create_time(time);
		note.setCn_note_last_modify_time(time);
		noteDao.save(note);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("创建笔记成功");
		result.setData(noteId);//返回笔记ID
		return result;
	}

	public NoteResult deleteNote(String noteId) {
		Note note = new Note();
		note.setCn_note_id(noteId);
		note.setCn_note_status_id("2");
		int rows = noteDao.dynamicUpdate(note);
		//		int rows = noteDao.updateStatus(noteId);
		NoteResult result = new NoteResult();
		if(rows==1){
			result.setStatus(0);
			result.setMsg("删除笔记成功");
		}else{
			result.setStatus(1);
			result.setMsg("删除笔记失败");
		}
		return result;
	}

	public NoteResult moveNote(String noteId, String bookId) {
		Note note = new Note();
		note.setCn_note_id(noteId);
		note.setCn_notebook_id(bookId);
//		noteDao.updateBookId(note);
		noteDao.dynamicUpdate(note);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("转移笔记成功");
		return result;
	}

	public NoteResult searchNote(
			String userId, String noteTitle,
			String noteStatus) {
		Note note = new Note();
		note.setCn_user_id(userId);
		//如果标题不是空,追加属性条件
		if(noteTitle !=null && !"".equals(noteTitle)){
			String keyword = "%"+noteTitle.trim()+"%";
			note.setCn_note_title(keyword);
		}
		//如果选择的不是"所有笔记",追加属性条件
		if(!"0".equals(noteStatus)){
			note.setCn_note_status_id(noteStatus);
		}
		List<Note> list = noteDao.findNotes(note);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}

	public NoteResult deleteNotes(String ids) {
		NoteResult result = new NoteResult();
		String[] noteIds = ids.split(";");
		int rows = noteDao.batchDelete(noteIds);
		if(rows==noteIds.length){
			result.setStatus(0);
			result.setMsg("删除成功");
		}else{
			result.setStatus(1);
			result.setMsg("删除失败");
		}
		return result;
	}

}



